#include "CPromiseHelper.h"
#include "CCurThreadTimerContext.h"
#include "../threadpool.h"
#include "ITimerContext.h"

namespace TB
{
	thread_local CCurThreadSyncContext CCurThreadSyncContext::m_oCurThreadSyncContext;
	thread_local CCurThreadTimerContext CCurThreadTimerContext::m_oCurThreadTimerContext;

	SVoidPromise CPromiseHelper::Delay(UINT64 qwMilliSeconds)
	{
		auto& roCurThreadTimerContext = CCurThreadTimerContext::GetCurrent();
		auto sPromiseProcessor = CreatePendingPromise<bool>();
		std::function<void()> cbFunc = [sPromiseProcessor]()
		{
			((SPromiseProcessor<bool>)sPromiseProcessor).Resolve(true);
		};
		auto& poTimerContext = roCurThreadTimerContext.GetTimerContext();
		if (poTimerContext == nullptr)
		{
			CThreadPool::StartTimer(qwMilliSeconds, cbFunc);
		}
		else
		{
			poTimerContext->StartTimer(qwMilliSeconds, cbFunc);
		}
		return sPromiseProcessor.GetPromise();
	}
}